# from loguru import logger
# import subprocess
# import os

# def execute_sh(cmd):
#     logger.info(cmd)
#     p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
#     logger.debug(type(p))
#     logger.debug(p)
#     for line in iter(p.stdout.readline, b''):
#         line = line.strip().decode("utf-8")
#         logger.debug(line)


# def get_app_names(directory):
#     """
#     返回值：返回名称，不是路径
#     """
#     app_paths = []
#     for root, dirs, files in os.walk(directory):
#         # 第一层目录
#         if root == directory:
#             logger.debug(f"root: {root}")
#             logger.debug(f"dirs: {dirs}")
#             # 判断有没有node_modules文件夹
#             for dir in dirs:
#                 if os.path.exists(os.path.join(root, dir, "node_modules")):
#                     app_paths.append(os.path.join(root, dir))
#                     logger.debug(f"files: {os.path.join(root, dir)}")
#                     # logger.debug(f"files: {files}")
#     logger.debug(f"app_paths: {app_paths}")
#     app_names = [path.split("/")[-1] for path in app_paths]
#     logger.debug(f"app_names: {app_names}")
#     return app_names

# def get_app_path():
#     # 遍历child_apps目录和main_apps目录，拿到该路径
#     # 跑出来的路径目录不对！是上一级目录了！
#     # child_apps_directory = os.path.join(os.getcwd(), 'child_apps')
#     # main_apps_directory = os.path.join(os.getcwd(), 'main_apps')
    
#     child_apps_directory = './child_apps'
#     main_apps_directory = './main_apps'
#     logger.debug(f"child_apps_directory: {child_apps_directory}")
#     logger.debug(f"main_apps_directory: {main_apps_directory}")
#     for c in child_apps_directory:
#         get_app_names(c)
#     for m in main_apps_directory:
#         get_app_names(m)


# def run_app_dev(app_name):
#     """
#     每个程序名,占用一个进程,执行yarn dev:{app_name}
#     需要开多进程
#     那app多的时候呢,线程池？
#     单线程执行 yarn dev:{app_name} 会不会被覆盖？手动执行时窗口在运行的，
#     怎么再运行，所以手动时多窗口运行，相当于多开多个进程
#     """
#     cmd = "yarn dev:{app_name}"
#     execute_sh(cmd)
    


# def run_app_prod():
#     pass


# if __name__ == '__main__':
#     get_app_path()


import os
import subprocess
from loguru import logger
import json

def execute_sh(cmd):
    logger.info(cmd)
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    logger.debug(type(p))
    logger.debug(p)
    for line in iter(p.stdout.readline, b''):
        line = line.strip().decode("utf-8")
        logger.debug(line)


def get_first_dirs(directory):
    first_dirs = []
    for root, dirs, files in os.walk(directory):
        # 第一层目录
        if root == directory:
            logger.debug(f"root: {root}")
            logger.debug(f"dirs: {dirs}")
            logger.debug(f"files: {files}")
            first_dirs.extend(dirs)
    return first_dirs

def save_app_name_with_json():
    """
    主从目录里只能放app目录,不能放其他文件
    """
    # 遍历child_apps目录和main_apps目录，拿到该路径
    child_apps_directory = os.path.join(os.getcwd(), 'child_apps')
    main_apps_directory = os.path.join(os.getcwd(), 'main_apps')

    child_first_dirs = get_first_dirs(child_apps_directory)
    main_first_dirs = get_first_dirs(main_apps_directory)
    logger.debug(f"child_first_dirs: {child_first_dirs}")
    logger.debug(f"main_first_dirs: {main_first_dirs}")

    app_json = {
        "child_apps": child_first_dirs,
        "main_apps": main_first_dirs
    }
    # 保存为json
    with open("app_names.json", "w") as f:
        # json.dumps 不行！
        json.dump(obj=app_json, fp=f, indent=4, ensure_ascii=False)


if __name__ == '__main__':
    save_app_name_with_json()
    
